$(document).ready(function() {
	$('#page').fullpage({
		menu: "#menu",
        navigation: true,
        loopBottom: true,
        anchors: ["home", "student", "teacher"],
        sectionsColor: ["none", "#fcfcfc", "#fcfcfc"],
	});
	
	$('#navbar-signin').click(function() {
		$('#nav-tab-signin').tab("show");
		$('#tab-signup').removeClass("active");
		$('#tab-signin').addClass("active");
	});
	$('#navbar-signup').click(function() {
		$('#nav-tab-signup').tab("show");
		$('#tab-signin').removeClass("active");
		$('#tab-signup').addClass("active");
	});

	var signIdentity = "学生";
	$('#sign-type label').click(function() { signIdentity = $(this).text();});
	$('#sign-student').click(function() { $('#signin-user').attr("placeholder", "学号 或 邮箱"); });
	$('#sign-teacher').click(function() { $('#signin-user').attr("placeholder", "工号 或 邮箱"); });
	$('#sign-admin').click(function() { $('#signin-user').attr("placeholder", "用户名"); });
	
	$('#signin-enter').click(function() {
		if (checkInput('#signin-user', '#signin-password', '#alert-signin', "请将登陆信息填写完整")) {
			$.post("user/login",
				{
					identity: signIdentity,
					username: $('#signin-user').val(),
					password: $('#signin-password').val()
				}, 
				function(json) {
					if (json.result == "success") {
						switch (signIdentity) {
							case "学生": window.location.href = "student.jsp"; break;
							case "教师": window.location.href = "teacher.jsp"; break;
							case "管理员": window.location.href = "admin.jsp"; break;
						}
					} else {
						showAlert('#alert-signin', "alert-danger", json.result);
					}
				}
			);
		}
	});
	$('#signup-enter').click(function() {
		if (checkInput('#signup-email', '#signup-ID', '#signup-password', '#alert-signup', "请将注册信息填写完整")) {
			if ($('#signup-password').val().length < 6) {
				showAlert('#alert-signup', "alert-danger", "密码必须大于6位");
				return;
			}
			$.post("user/register",
				{
					email: $('#signup-email').val(),
					ID: $('#signup-ID').val(),
					password: $('#signup-password').val()
				}, 
				function(json) {
					if (json.result == "success") {
						showAlert('#alert-signin', "alert-success", "用户"+$('#signup-ID').val()+"注册成功, 请登录");
						$('#navbar-signin').click();
					} else {
						showAlert('#alert-signup', "alert-danger", json.result);
					}
				}
			);
		}
	});
	$('#send-enter').click(function() {
		if (checkInput('#send-user', '#send-email', '#alert-send', "请将用户信息填写完整")) {
			$.post("user/check",
				{
					ID: $('#send-user').val(),
					email: $('#send-email').val()
				}, 
				function(json) {
					if (json.result == "success") {
						$('#tab-send').removeClass("active");
						$('#tab-reset').addClass("active");
					} else {
						showAlert('#alert-send', "alert-danger", json.result);
					}
				}
			);
		}
	});
	$('#reset-enter').click(function() {
		if (checkInput('#reset-password', '#reset-check', '#reset-code', '#alert-reset', "请将信息填写完整")) {
			$.post("",
				{
					ID: $('#send-user').val(),
					email: $('#send-email').val(),
					code: $('#reset-code').val(),
					password: $('#reset-password').val()
				}, 
				function(json) {
					if (json.result == "success") {
						$('#tab-reset').removeClass("active");
						$('#tab-home').addClass("active");
						showAlert('#alert-signin', "alert-success", "用户"+$('#send-user').val()+"密码重设成功, 请登录");
					} else {
						showAlert('#alert-reset', "alert-danger", json.result);
					}
				}
			);
		}
	});
});